DodoRunner.Obstacle = (function ($, _, app, easel) {

    var TYPE_CIRCLE = "leCircle";
    var TYPE_RECT = "leRect";
    var TYPE_SPIKES = "leSpikes";

    var createCircle = function (xPos, yPos, radius, rollSpeed) {
        return create(TYPE_CIRCLE, {"x":-app.Canvas.getCameraX()+1280, "y":Math.random()*570, "width":120, "height":119});
    },

    createFallingBlock = function () {
        return create(TYPE_RECT, {"x":-app.Canvas.getCameraX()+1280, "y":-48, "width":90, "height":90});
    },

    createSpikes = function (xPos, yPos, width, height) {
        return create(TYPE_SPIKES, {"x":-app.Canvas.getCameraX()+1280, "y":580, "width":90, "height":60});
    },

    create = function (type, attrs) {
        if (type == TYPE_CIRCLE) {
            obj = new DodoRunner.Circle();
            obj.TYPE = TYPE_RECT;
            obj.x = attrs["x"];
            obj.y = attrs["y"];
            obj.width = attrs["width"];
            obj.height = attrs["height"];
            obj.init();
            app.Collision.registerObstacle(obj);
            return obj;
        }
        if (type == TYPE_RECT) {
            obj = new DodoRunner.Rectangle();
            obj.TYPE = TYPE_RECT;
            obj.x = attrs["x"];
            obj.y = attrs["y"];
            obj.width = attrs["width"];
            obj.height = attrs["height"];
            obj.init();
            app.Collision.registerObstacle(obj);
            return obj;
        }
        if (type == TYPE_SPIKES) {
            obj = new DodoRunner.Spikes();
            obj.TYPE = TYPE_SPIKES;
            obj.x = attrs["x"];
            obj.y = attrs["y"];
            obj.width = attrs["width"];
            obj.height = attrs["height"];
            obj.init();
            app.Collision.registerObstacle(obj);
            return obj;
        }
    };
    return {
        TYPE_CIRCLE : TYPE_CIRCLE,
        TYPE_RECT : TYPE_RECT,
        TYPE_SPIKES : TYPE_SPIKES,
        createSpikes:createSpikes,
        createFallingBlock:createFallingBlock,
        createSphere:createCircle
    }
}(jQuery, _, DodoRunner, createjs));

DodoRunner.Circle = function(){
    var that = {};
    that.ROLLSPEED = 48;

    that.spriteSheet = undefined;
    that.sprite = undefined;

    that.init = function() {
        that.createSprite();
        that.animate();
    };

    that.createSprite = function(){
        that.spriteSheet = new createjs.SpriteSheet({
            images: [DodoRunner.AssetManager.getImageByName("circle")],
            frames: {
                width: that.width,
                height: that.height
            },
            animations: {
                roll	:[ 0,  5]
            }
        });
        sprite = new createjs.BitmapAnimation(that.spriteSheet);
        sprite.gotoAndPlay("roll");
        sprite.x = that.x;
        sprite.y = that.y;
        sprite.z = 3;
        DodoRunner.Canvas.addChild(sprite);
        that.sprite = sprite;
    };

    that.rollLeft = function(dt){
        that.x -= 10;
        that.sprite.x = that.x;
        requestAnimationFrame(that.rollLeft);
    };
    that.animate = function(){
        that.rollLeft(0);
    };

    return that;
};

DodoRunner.Spikes= function(){
    var that = {};

    that.bitmap = undefined;

    that.init = function() {
        that.createSprite();
    };

    that.createSprite = function(){
        that.bitmap = new createjs.Bitmap(DodoRunner.AssetManager.getImageByName("triangle"));
        that.bitmap.x = that.x;
        that.bitmap.y = that.y;
        that.bitmap.z = 0;
        DodoRunner.Canvas.addChild(that.bitmap);
    };

    return that;
};

DodoRunner.Rectangle= function(){
    var that = {};

    that.FALLSPEED = 48;
    that.LIMIT = 580;

    that.bitmap = undefined;

    that.init = function() {
        that.createSprite();
        that.animate();
    };

    that.fallDown = function(dt){
        console.log("falldown", dt, that);
        if (that.y <=  that.LIMIT){
            that.y += 10; //that.y += (Date.now()-dt)/1000*that.FALLSPEED;
            that.bitmap.y = that.y;
            requestAnimationFrame(that.fallDown);
        }
    };

    that.animate = function(){
        that.fallDown(0);
    };

    that.createSprite = function(){
        that.bitmap = new createjs.Bitmap(DodoRunner.AssetManager.getImageByName("quad"));
        that.bitmap.x = that.x;
        that.bitmap.y = that.y;
        console.log("init", that.y, that);
        that.bitmap.z = 0;
        DodoRunner.Canvas.addChild(that.bitmap);
    };

    return that;
};